/****************************************************************
 * Fulguro
 * Copyright (C) 2004 Christophe Clienti
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 ***************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef __SSE2__
#include <emmintrin.h>
#include <xmmintrin.h>
#endif

#include "flgrCoreDispatch.h"
#include "flgrCoreReplaceFast.h"


void flgr1d_replace_I_LE_I_I_I_fast_fgUINT8(FLGR_Data1D *datout,
					    FLGR_Data1D *dattest1, FLGR_Data1D *dattest2,
					    FLGR_Data1D *dattrue, FLGR_Data1D *datfalse) {
#ifdef __SSE2__
  int j;
  fgUINT8 *rowptr_out,*rowptr_t1,*rowptr_t2;
  fgUINT8 *rowptr_true, *rowptr_false;

  __m128i ptst1, ptst2, ptrue, pfalse;
  __m128i mask,one;

  FLGR_DECLARE_FUNCTION;

  one = _mm_set1_epi8((char) 0xFF);

  rowptr_out = (fgUINT8 *) datout->array;
  rowptr_t1 = (fgUINT8 *) dattest1->array;
  rowptr_t2 = (fgUINT8 *) dattest2->array;
  rowptr_true = (fgUINT8 *) dattrue->array;
  rowptr_false = (fgUINT8 *) datfalse->array;


  for(j=0 ; j<datout->length ; j+=16) {
    ptst1  = _mm_load_si128((__m128i*) rowptr_t1);
    ptst2  = _mm_load_si128((__m128i*) rowptr_t2);
    ptrue  = _mm_load_si128((__m128i*) rowptr_true);
    pfalse = _mm_load_si128((__m128i*) rowptr_false);

    ptst1 = _mm_max_epu8(ptst1,ptst2);
    mask = _mm_cmpeq_epi8(ptst1,ptst2);
    _mm_maskmoveu_si128(ptrue,mask, (char*) rowptr_out);
    //_mm_mfence();
    mask = _mm_andnot_si128(mask,one);
    _mm_maskmoveu_si128(pfalse,mask, (char*) rowptr_out);

    rowptr_false+=16;
    rowptr_true+=16;
    rowptr_t2+=16;
    rowptr_t1+=16;
    rowptr_out+=16;
  }
#endif
}
void flgr1d_replace_I_EQ_I_I_I_fast_fgUINT8(FLGR_Data1D *datout,
					    FLGR_Data1D *dattest1, FLGR_Data1D *dattest2,
					    FLGR_Data1D *dattrue, FLGR_Data1D *datfalse) {
#ifdef __SSE2__
  int j;
  fgUINT8 *rowptr_out,*rowptr_t1,*rowptr_t2;
  fgUINT8 *rowptr_true, *rowptr_false;

  __m128i ptst1, ptst2, ptrue, pfalse;
  __m128i mask,one;

  FLGR_DECLARE_FUNCTION;

  one = _mm_set1_epi8((char) 0xFF);

  rowptr_out = (fgUINT8 *) datout->array;
  rowptr_t1 = (fgUINT8 *) dattest1->array;
  rowptr_t2 = (fgUINT8 *) dattest2->array;
  rowptr_true = (fgUINT8 *) dattrue->array;
  rowptr_false = (fgUINT8 *) datfalse->array;

  for(j=0 ; j<datout->length ; j+=16) {
    ptst1  = _mm_load_si128((__m128i*) rowptr_t1);
    ptst2  = _mm_load_si128((__m128i*) rowptr_t2);
    ptrue  = _mm_load_si128((__m128i*) rowptr_true);
    pfalse = _mm_load_si128((__m128i*) rowptr_false);


    mask = _mm_cmpeq_epi8(ptst1,ptst2);
    _mm_maskmoveu_si128(ptrue,mask, (char*) rowptr_out);
    //_mm_mfence();
    mask = _mm_andnot_si128(mask,one);
    _mm_maskmoveu_si128(pfalse,mask, (char*) rowptr_out);

    rowptr_false+=16;
    rowptr_true+=16;
    rowptr_t2+=16;
    rowptr_t1+=16;
    rowptr_out+=16;
  }
#endif
}
void flgr1d_replace_I_NE_I_I_I_fast_fgUINT8(FLGR_Data1D *datout,
					    FLGR_Data1D *dattest1, FLGR_Data1D *dattest2,
					    FLGR_Data1D *dattrue, FLGR_Data1D *datfalse) {
#ifdef __SSE2__
  int j;
  fgUINT8 *rowptr_out,*rowptr_t1,*rowptr_t2;
  fgUINT8 *rowptr_true, *rowptr_false;

  __m128i ptst1, ptst2, ptrue, pfalse;
  __m128i mask,one;

  FLGR_DECLARE_FUNCTION;

  one = _mm_set1_epi8((char) 0xFF);

  rowptr_out = (fgUINT8 *) datout->array;
  rowptr_t1 = (fgUINT8 *) dattest1->array;
  rowptr_t2 = (fgUINT8 *) dattest2->array;
  rowptr_true = (fgUINT8 *) dattrue->array;
  rowptr_false = (fgUINT8 *) datfalse->array;


  for(j=0 ; j<datout->length ; j+=16) {
    ptst1  = _mm_load_si128((__m128i*) rowptr_t1);
    ptst2  = _mm_load_si128((__m128i*) rowptr_t2);
    ptrue  = _mm_load_si128((__m128i*) rowptr_true);
    pfalse = _mm_load_si128((__m128i*) rowptr_false);

    mask = _mm_cmpeq_epi8(ptst1,ptst2);
    _mm_maskmoveu_si128(pfalse,mask, (char*) rowptr_out);
    //_mm_mfence();
    mask = _mm_andnot_si128(mask,one);
    _mm_maskmoveu_si128(ptrue,mask, (char*) rowptr_out);


    rowptr_false+=16;
    rowptr_true+=16;
    rowptr_t2+=16;
    rowptr_t1+=16;
    rowptr_out+=16;
  }
#endif
}
void flgr1d_replace_I_GE_I_I_I_fast_fgUINT8(FLGR_Data1D *datout,
					    FLGR_Data1D *dattest1, FLGR_Data1D *dattest2,
					    FLGR_Data1D *dattrue, FLGR_Data1D *datfalse) {
#ifdef __SSE2__
  int j;
  fgUINT8 *rowptr_out,*rowptr_t1,*rowptr_t2;
  fgUINT8 *rowptr_true, *rowptr_false;

  __m128i ptst1, ptst2, ptrue, pfalse;
  __m128i mask,one;

  FLGR_DECLARE_FUNCTION;

  one = _mm_set1_epi8((char) 0xFF);

  rowptr_out = (fgUINT8 *) datout->array;
  rowptr_t1 = (fgUINT8 *) dattest1->array;
  rowptr_t2 = (fgUINT8 *) dattest2->array;
  rowptr_true = (fgUINT8 *) dattrue->array;
  rowptr_false = (fgUINT8 *) datfalse->array;


  for(j=0 ; j<datout->length ; j+=16) {
    ptst1  = _mm_load_si128((__m128i*) rowptr_t1);
    ptst2  = _mm_load_si128((__m128i*) rowptr_t2);
    ptrue  = _mm_load_si128((__m128i*) rowptr_true);
    pfalse = _mm_load_si128((__m128i*) rowptr_false);

    ptst1 = _mm_min_epu8(ptst1,ptst2);
    mask = _mm_cmpeq_epi8(ptst1,ptst2);
    _mm_maskmoveu_si128(ptrue,mask, (char*) rowptr_out);
    //_mm_mfence();
    mask = _mm_andnot_si128(mask,one);
    _mm_maskmoveu_si128(pfalse,mask, (char*) rowptr_out);


    rowptr_false+=16;
    rowptr_true+=16;
    rowptr_t2+=16;
    rowptr_t1+=16;
    rowptr_out+=16;
  }
#endif
}








void flgr1d_replace_I_LE_I_I_C_fast_fgUINT8(FLGR_Data1D *datout,
					    FLGR_Data1D *dattest1, FLGR_Data1D *dattest2,
					    FLGR_Data1D *dattrue, fgUINT8 vfalse) {
#ifdef __SSE2__
  int j;
  fgUINT8 *rowptr_out,*rowptr_t1,*rowptr_t2;
  fgUINT8 *rowptr_true;

  __m128i ptst1, ptst2, ptrue, pfalse;
  __m128i mask,one;

  FLGR_DECLARE_FUNCTION;

  one = _mm_set1_epi8((char) 0xFF);
  pfalse = _mm_set1_epi8((char) vfalse);

  rowptr_out = (fgUINT8 *) datout->array;
  rowptr_t1 = (fgUINT8 *) dattest1->array;
  rowptr_t2 = (fgUINT8 *) dattest2->array;
  rowptr_true = (fgUINT8 *) dattrue->array;


  for(j=0 ; j<datout->length ; j+=16) {
    ptst1  = _mm_load_si128((__m128i*) rowptr_t1);
    ptst2  = _mm_load_si128((__m128i*) rowptr_t2);
    ptrue  = _mm_load_si128((__m128i*) rowptr_true);

    ptst1 = _mm_max_epu8(ptst1,ptst2);
    mask = _mm_cmpeq_epi8(ptst1,ptst2);
    _mm_maskmoveu_si128(ptrue,mask, (char*) rowptr_out);
    //_mm_mfence();
    mask = _mm_andnot_si128(mask,one);
    _mm_maskmoveu_si128(pfalse,mask, (char*) rowptr_out);


    rowptr_true+=16;
    rowptr_t2+=16;
    rowptr_t1+=16;
    rowptr_out+=16;
  }
#endif
}
void flgr1d_replace_I_EQ_I_I_C_fast_fgUINT8(FLGR_Data1D *datout,
					    FLGR_Data1D *dattest1, FLGR_Data1D *dattest2,
					    FLGR_Data1D *dattrue, fgUINT8 vfalse) {
#ifdef __SSE2__
  int j;
  fgUINT8 *rowptr_out,*rowptr_t1,*rowptr_t2;
  fgUINT8 *rowptr_true;

  __m128i ptst1, ptst2, ptrue, pfalse;
  __m128i mask,one;

  FLGR_DECLARE_FUNCTION;

  one = _mm_set1_epi8((char) 0xFF);
  pfalse = _mm_set1_epi8((char) vfalse);

  rowptr_out = (fgUINT8 *) datout->array;
  rowptr_t1 = (fgUINT8 *) dattest1->array;
  rowptr_t2 = (fgUINT8 *) dattest2->array;
  rowptr_true = (fgUINT8 *) dattrue->array;


  for(j=0 ; j<datout->length ; j+=16) {
    ptst1  = _mm_load_si128((__m128i*) rowptr_t1);
    ptst2  = _mm_load_si128((__m128i*) rowptr_t2);
    ptrue  = _mm_load_si128((__m128i*) rowptr_true);

    mask = _mm_cmpeq_epi8(ptst1,ptst2);
    _mm_maskmoveu_si128(ptrue,mask, (char*) rowptr_out);
    //_mm_mfence();
    mask = _mm_andnot_si128(mask,one);
    _mm_maskmoveu_si128(pfalse,mask, (char*) rowptr_out);


    rowptr_true+=16;
    rowptr_t2+=16;
    rowptr_t1+=16;
    rowptr_out+=16;
  }
#endif
}
void flgr1d_replace_I_NE_I_I_C_fast_fgUINT8(FLGR_Data1D *datout,
					    FLGR_Data1D *dattest1, FLGR_Data1D *dattest2,
					    FLGR_Data1D *dattrue, fgUINT8 vfalse) {
#ifdef __SSE2__
  int j;
  fgUINT8 *rowptr_out,*rowptr_t1,*rowptr_t2;
  fgUINT8 *rowptr_true;

  __m128i ptst1, ptst2, ptrue, pfalse;
  __m128i mask,one;

  FLGR_DECLARE_FUNCTION;

  one = _mm_set1_epi8((char) 0xFF);
  pfalse = _mm_set1_epi8((char) vfalse);

  rowptr_out = (fgUINT8 *) datout->array;
  rowptr_t1 = (fgUINT8 *) dattest1->array;
  rowptr_t2 = (fgUINT8 *) dattest2->array;
  rowptr_true = (fgUINT8 *) dattrue->array;


  for(j=0 ; j<datout->length ; j+=16) {
    ptst1  = _mm_load_si128((__m128i*) rowptr_t1);
    ptst2  = _mm_load_si128((__m128i*) rowptr_t2);
    ptrue  = _mm_load_si128((__m128i*) rowptr_true);

    mask = _mm_cmpeq_epi8(ptst1,ptst2);
    _mm_maskmoveu_si128(pfalse,mask, (char*) rowptr_out);
    //_mm_mfence();
    mask = _mm_andnot_si128(mask,one);
    _mm_maskmoveu_si128(ptrue,mask, (char*) rowptr_out);


    rowptr_true+=16;
    rowptr_t2+=16;
    rowptr_t1+=16;
    rowptr_out+=16;
  }
#endif
}
void flgr1d_replace_I_GE_I_I_C_fast_fgUINT8(FLGR_Data1D *datout,
					    FLGR_Data1D *dattest1, FLGR_Data1D *dattest2,
					    FLGR_Data1D *dattrue, fgUINT8 vfalse) {
#ifdef __SSE2__
  int j;
  fgUINT8 *rowptr_out,*rowptr_t1,*rowptr_t2;
  fgUINT8 *rowptr_true;

  __m128i ptst1, ptst2, ptrue, pfalse;
  __m128i mask,one;

  FLGR_DECLARE_FUNCTION;

  one = _mm_set1_epi8((char) 0xFF);
  pfalse = _mm_set1_epi8((char) vfalse);

  rowptr_out = (fgUINT8 *) datout->array;
  rowptr_t1 = (fgUINT8 *) dattest1->array;
  rowptr_t2 = (fgUINT8 *) dattest2->array;
  rowptr_true = (fgUINT8 *) dattrue->array;


  for(j=0 ; j<datout->length ; j+=16) {
    ptst1  = _mm_load_si128((__m128i*) rowptr_t1);
    ptst2  = _mm_load_si128((__m128i*) rowptr_t2);
    ptrue  = _mm_load_si128((__m128i*) rowptr_true);

    ptst1 = _mm_min_epu8(ptst1,ptst2);
    mask = _mm_cmpeq_epi8(ptst1,ptst2);
    _mm_maskmoveu_si128(ptrue,mask, (char*) rowptr_out);
    //_mm_mfence();
    mask = _mm_andnot_si128(mask,one);
    _mm_maskmoveu_si128(pfalse,mask, (char*) rowptr_out);


    rowptr_true+=16;
    rowptr_t2+=16;
    rowptr_t1+=16;
    rowptr_out+=16;
  }
#endif
}










void flgr1d_replace_I_LE_I_C_I_fast_fgUINT8(FLGR_Data1D *datout,
					    FLGR_Data1D *dattest1, FLGR_Data1D *dattest2,
					    fgUINT8 vtrue, FLGR_Data1D *datfalse) {
#ifdef __SSE2__
  int j;
  fgUINT8 *rowptr_out,*rowptr_t1,*rowptr_t2;
  fgUINT8 *rowptr_false;

  __m128i ptst1, ptst2, ptrue, pfalse;
  __m128i mask,one;

  FLGR_DECLARE_FUNCTION;

  one = _mm_set1_epi8((char) 0xFF);
  ptrue = _mm_set1_epi8((char) vtrue);

  rowptr_out = (fgUINT8 *) datout->array;
  rowptr_t1 = (fgUINT8 *) dattest1->array;
  rowptr_t2 = (fgUINT8 *) dattest2->array;
  rowptr_false = (fgUINT8 *) datfalse->array;


  for(j=0 ; j<datout->length ; j+=16) {
    ptst1  = _mm_load_si128((__m128i*) rowptr_t1);
    ptst2  = _mm_load_si128((__m128i*) rowptr_t2);
    pfalse  = _mm_load_si128((__m128i*) rowptr_false);

    ptst1 = _mm_max_epu8(ptst1,ptst2);
    mask = _mm_cmpeq_epi8(ptst1,ptst2);
    _mm_maskmoveu_si128(ptrue,mask, (char*) rowptr_out);
    //_mm_mfence();
    mask = _mm_andnot_si128(mask,one);
    _mm_maskmoveu_si128(pfalse,mask, (char*) rowptr_out);


    rowptr_false+=16;
    rowptr_t2+=16;
    rowptr_t1+=16;
    rowptr_out+=16;
  }
#endif
}
void flgr1d_replace_I_EQ_I_C_I_fast_fgUINT8(FLGR_Data1D *datout,
					    FLGR_Data1D *dattest1, FLGR_Data1D *dattest2,
					    fgUINT8 vtrue, FLGR_Data1D *datfalse) {
#ifdef __SSE2__
  int j;
  fgUINT8 *rowptr_out,*rowptr_t1,*rowptr_t2;
  fgUINT8 *rowptr_false;

  __m128i ptst1, ptst2, ptrue, pfalse;
  __m128i mask,one;

  FLGR_DECLARE_FUNCTION;

  one = _mm_set1_epi8((char) 0xFF);
  ptrue = _mm_set1_epi8((char) vtrue);

  rowptr_out = (fgUINT8 *) datout->array;
  rowptr_t1 = (fgUINT8 *) dattest1->array;
  rowptr_t2 = (fgUINT8 *) dattest2->array;
  rowptr_false = (fgUINT8 *) datfalse->array;


  for(j=0 ; j<datout->length ; j+=16) {
    ptst1  = _mm_load_si128((__m128i*) rowptr_t1);
    ptst2  = _mm_load_si128((__m128i*) rowptr_t2);
    pfalse = _mm_load_si128((__m128i*) rowptr_false);

    mask = _mm_cmpeq_epi8(ptst1,ptst2);
    _mm_maskmoveu_si128(ptrue,mask, (char*) rowptr_out);
    //_mm_mfence();
    mask = _mm_andnot_si128(mask,one);
    _mm_maskmoveu_si128(pfalse,mask, (char*) rowptr_out);


    rowptr_false+=16;
    rowptr_t2+=16;
    rowptr_t1+=16;
    rowptr_out+=16;
  }
#endif
}
void flgr1d_replace_I_NE_I_C_I_fast_fgUINT8(FLGR_Data1D *datout,
					    FLGR_Data1D *dattest1, FLGR_Data1D *dattest2,
					    fgUINT8 vtrue, FLGR_Data1D *datfalse) {
#ifdef __SSE2__
  int j;
  fgUINT8 *rowptr_out,*rowptr_t1,*rowptr_t2;
  fgUINT8 *rowptr_false;

  __m128i ptst1, ptst2, ptrue, pfalse;
  __m128i mask,one;

  FLGR_DECLARE_FUNCTION;

  one = _mm_set1_epi8((char) 0xFF);
  ptrue = _mm_set1_epi8((char) vtrue);

  rowptr_out = (fgUINT8 *) datout->array;
  rowptr_t1 = (fgUINT8 *) dattest1->array;
  rowptr_t2 = (fgUINT8 *) dattest2->array;
  rowptr_false = (fgUINT8 *) datfalse->array;


  for(j=0 ; j<datout->length ; j+=16) {
    ptst1  = _mm_load_si128((__m128i*) rowptr_t1);
    ptst2  = _mm_load_si128((__m128i*) rowptr_t2);
    pfalse = _mm_load_si128((__m128i*) rowptr_false);

    mask = _mm_cmpeq_epi8(ptst1,ptst2);
    _mm_maskmoveu_si128(pfalse,mask, (char*) rowptr_out);
    //_mm_mfence();
    mask = _mm_andnot_si128(mask,one);
    _mm_maskmoveu_si128(ptrue,mask, (char*) rowptr_out);


    rowptr_false+=16;
    rowptr_t2+=16;
    rowptr_t1+=16;
    rowptr_out+=16;
  }
#endif
}
void flgr1d_replace_I_GE_I_C_I_fast_fgUINT8(FLGR_Data1D *datout,
					    FLGR_Data1D *dattest1, FLGR_Data1D *dattest2,
					    fgUINT8 vtrue, FLGR_Data1D *datfalse) {
#ifdef __SSE2__
  int j;
  fgUINT8 *rowptr_out,*rowptr_t1,*rowptr_t2;
  fgUINT8 *rowptr_false;

  __m128i ptst1, ptst2, ptrue, pfalse;
  __m128i mask,one;

  FLGR_DECLARE_FUNCTION;

  one = _mm_set1_epi8((char) 0xFF);
  ptrue = _mm_set1_epi8((char) vtrue);

  rowptr_out = (fgUINT8 *) datout->array;
  rowptr_t1 = (fgUINT8 *) dattest1->array;
  rowptr_t2 = (fgUINT8 *) dattest2->array;
  rowptr_false = (fgUINT8 *) datfalse->array;


  for(j=0 ; j<datout->length ; j+=16) {
    ptst1  = _mm_load_si128((__m128i*) rowptr_t1);
    ptst2  = _mm_load_si128((__m128i*) rowptr_t2);
    pfalse  = _mm_load_si128((__m128i*) rowptr_false);

    ptst1 = _mm_min_epu8(ptst1,ptst2);
    mask = _mm_cmpeq_epi8(ptst1,ptst2);
    _mm_maskmoveu_si128(ptrue,mask, (char*) rowptr_out);
    //_mm_mfence();
    mask = _mm_andnot_si128(mask,one);
    _mm_maskmoveu_si128(pfalse,mask, (char*) rowptr_out);


    rowptr_false+=16;
    rowptr_t2+=16;
    rowptr_t1+=16;
    rowptr_out+=16;
  }
#endif
}









void flgr1d_replace_I_LE_I_C_C_fast_fgUINT8(FLGR_Data1D *datout,
					    FLGR_Data1D *dattest1, FLGR_Data1D *dattest2,
					    fgUINT8 vtrue, fgUINT8 vfalse) {
#ifdef __SSE2__
  int j;
  fgUINT8 *rowptr_out,*rowptr_t1,*rowptr_t2;

  __m128i ptst1, ptst2, ptrue, pfalse;
  __m128i mask,one;

  FLGR_DECLARE_FUNCTION;

  one = _mm_set1_epi8((char) 0xFF);
  ptrue = _mm_set1_epi8((char) vtrue);
  pfalse = _mm_set1_epi8((char) vfalse);

  rowptr_out = (fgUINT8 *) datout->array;
  rowptr_t1 = (fgUINT8 *) dattest1->array;
  rowptr_t2 = (fgUINT8 *) dattest2->array;


  for(j=0 ; j<datout->length ; j+=16) {
    ptst1  = _mm_load_si128((__m128i*) rowptr_t1);
    ptst2  = _mm_load_si128((__m128i*) rowptr_t2);

    ptst1 = _mm_max_epu8(ptst1,ptst2);
    mask = _mm_cmpeq_epi8(ptst1,ptst2);
    _mm_maskmoveu_si128(ptrue,mask, (char*) rowptr_out);
    //_mm_mfence();
    mask = _mm_andnot_si128(mask,one);
    _mm_maskmoveu_si128(pfalse,mask, (char*) rowptr_out);


    rowptr_t2+=16;
    rowptr_t1+=16;
    rowptr_out+=16;
  }
#endif
}
void flgr1d_replace_I_EQ_I_C_C_fast_fgUINT8(FLGR_Data1D *datout,
					    FLGR_Data1D *dattest1, FLGR_Data1D *dattest2,
					    fgUINT8 vtrue, fgUINT8 vfalse) {
#ifdef __SSE2__
  int j;
  fgUINT8 *rowptr_out,*rowptr_t1,*rowptr_t2;

  __m128i ptst1, ptst2, ptrue, pfalse;
  __m128i mask,one;

  FLGR_DECLARE_FUNCTION;

  one = _mm_set1_epi8((char) 0xFF);
  ptrue = _mm_set1_epi8((char) vtrue);
  pfalse = _mm_set1_epi8((char) vfalse);

  rowptr_out = (fgUINT8 *) datout->array;
  rowptr_t1 = (fgUINT8 *) dattest1->array;
  rowptr_t2 = (fgUINT8 *) dattest2->array;


  for(j=0 ; j<datout->length ; j+=16) {
    ptst1  = _mm_load_si128((__m128i*) rowptr_t1);
    ptst2  = _mm_load_si128((__m128i*) rowptr_t2);

    mask = _mm_cmpeq_epi8(ptst1,ptst2);
    _mm_maskmoveu_si128(ptrue,mask, (char*) rowptr_out);
    //_mm_mfence();
    mask = _mm_andnot_si128(mask,one);
    _mm_maskmoveu_si128(pfalse,mask, (char*) rowptr_out);


    rowptr_t2+=16;
    rowptr_t1+=16;
    rowptr_out+=16;
  }
#endif
}
void flgr1d_replace_I_NE_I_C_C_fast_fgUINT8(FLGR_Data1D *datout,
					    FLGR_Data1D *dattest1, FLGR_Data1D *dattest2,
					    fgUINT8 vtrue, fgUINT8 vfalse) {
#ifdef __SSE2__
  int j;
  fgUINT8 *rowptr_out,*rowptr_t1,*rowptr_t2;

  __m128i ptst1, ptst2, ptrue, pfalse;
  __m128i mask,one;

  FLGR_DECLARE_FUNCTION;

  one = _mm_set1_epi8((char) 0xFF);
  ptrue = _mm_set1_epi8((char) vtrue);
  pfalse = _mm_set1_epi8((char) vfalse);

  rowptr_out = (fgUINT8 *) datout->array;
  rowptr_t1 = (fgUINT8 *) dattest1->array;
  rowptr_t2 = (fgUINT8 *) dattest2->array;


  for(j=0 ; j<datout->length ; j+=16) {
    ptst1  = _mm_load_si128((__m128i*) rowptr_t1);
    ptst2  = _mm_load_si128((__m128i*) rowptr_t2);

    mask = _mm_cmpeq_epi8(ptst1,ptst2);
    _mm_maskmoveu_si128(pfalse,mask, (char*) rowptr_out);
    //_mm_mfence();
    mask = _mm_andnot_si128(mask,one);
    _mm_maskmoveu_si128(ptrue,mask, (char*) rowptr_out);


    rowptr_t2+=16;
    rowptr_t1+=16;
    rowptr_out+=16;
  }
#endif
}
void flgr1d_replace_I_GE_I_C_C_fast_fgUINT8(FLGR_Data1D *datout,
					    FLGR_Data1D *dattest1, FLGR_Data1D *dattest2,
					    fgUINT8 vtrue, fgUINT8 vfalse) {
#ifdef __SSE2__
  int j;
  fgUINT8 *rowptr_out,*rowptr_t1,*rowptr_t2;

  __m128i ptst1, ptst2, ptrue, pfalse;
  __m128i mask,one;

  FLGR_DECLARE_FUNCTION;

  one = _mm_set1_epi8((char) 0xFF);
  ptrue = _mm_set1_epi8((char) vtrue);
  pfalse = _mm_set1_epi8((char) vfalse);

  rowptr_out = (fgUINT8 *) datout->array;
  rowptr_t1 = (fgUINT8 *) dattest1->array;
  rowptr_t2 = (fgUINT8 *) dattest2->array;


  for(j=0 ; j<datout->length ; j+=16) {
    ptst1  = _mm_load_si128((__m128i*) rowptr_t1);
    ptst2  = _mm_load_si128((__m128i*) rowptr_t2);

    ptst1 = _mm_min_epu8(ptst1,ptst2);
    mask = _mm_cmpeq_epi8(ptst1,ptst2);
    _mm_maskmoveu_si128(ptrue,mask, (char*) rowptr_out);
    //_mm_mfence();
    mask = _mm_andnot_si128(mask,one);
    _mm_maskmoveu_si128(pfalse,mask, (char*) rowptr_out);


    rowptr_t2+=16;
    rowptr_t1+=16;
    rowptr_out+=16;
  }
#endif
}











void flgr1d_replace_I_LE_C_I_I_fast_fgUINT8(FLGR_Data1D *datout,
					    FLGR_Data1D *dattest1,fgUINT8 vtest2,
					    FLGR_Data1D *dattrue, FLGR_Data1D *datfalse) {
#ifdef __SSE2__
  int j;
  fgUINT8 *rowptr_out,*rowptr_t1;
  fgUINT8 *rowptr_true, *rowptr_false;

  __m128i ptst1, ptst2, ptrue, pfalse;
  __m128i mask,one;

  FLGR_DECLARE_FUNCTION;

  one = _mm_set1_epi8((char) 0xFF);
  ptst2 = _mm_set1_epi8((char) vtest2);

  rowptr_out = (fgUINT8 *) datout->array;
  rowptr_t1 = (fgUINT8 *) dattest1->array;
  rowptr_true = (fgUINT8 *) dattrue->array;
  rowptr_false = (fgUINT8 *) datfalse->array;


  for(j=0 ; j<datout->length ; j+=16) {
    ptst1  = _mm_load_si128((__m128i*) rowptr_t1);
    ptrue  = _mm_load_si128((__m128i*) rowptr_true);
    pfalse = _mm_load_si128((__m128i*) rowptr_false);

    ptst1 = _mm_max_epu8(ptst1,ptst2);
    mask = _mm_cmpeq_epi8(ptst1,ptst2);
    _mm_maskmoveu_si128(ptrue,mask, (char*) rowptr_out);
    //_mm_mfence();
    mask = _mm_andnot_si128(mask,one);
    _mm_maskmoveu_si128(pfalse,mask, (char*) rowptr_out);


    rowptr_false+=16;
    rowptr_true+=16;
    rowptr_t1+=16;
    rowptr_out+=16;
  }
#endif
}
void flgr1d_replace_I_EQ_C_I_I_fast_fgUINT8(FLGR_Data1D *datout,
					    FLGR_Data1D *dattest1,fgUINT8 vtest2,
					    FLGR_Data1D *dattrue, FLGR_Data1D *datfalse) {
#ifdef __SSE2__
  int j;
  fgUINT8 *rowptr_out,*rowptr_t1;
  fgUINT8 *rowptr_true, *rowptr_false;

  __m128i ptst1, ptst2, ptrue, pfalse;
  __m128i mask,one;

  FLGR_DECLARE_FUNCTION;

  one = _mm_set1_epi8((char) 0xFF);
  ptst2 = _mm_set1_epi8((char) vtest2);

  rowptr_out = (fgUINT8 *) datout->array;
  rowptr_t1 = (fgUINT8 *) dattest1->array;
  rowptr_true = (fgUINT8 *) dattrue->array;
  rowptr_false = (fgUINT8 *) datfalse->array;


  for(j=0 ; j<datout->length ; j+=16) {
    ptst1  = _mm_load_si128((__m128i*) rowptr_t1);
    ptrue  = _mm_load_si128((__m128i*) rowptr_true);
    pfalse = _mm_load_si128((__m128i*) rowptr_false);

    mask = _mm_cmpeq_epi8(ptst1,ptst2);
    _mm_maskmoveu_si128(ptrue,mask, (char*) rowptr_out);
    //_mm_mfence();
    mask = _mm_andnot_si128(mask,one);
    _mm_maskmoveu_si128(pfalse,mask, (char*) rowptr_out);


    rowptr_false+=16;
    rowptr_true+=16;
    rowptr_t1+=16;
    rowptr_out+=16;
  }
#endif
}
void flgr1d_replace_I_NE_C_I_I_fast_fgUINT8(FLGR_Data1D *datout,
					    FLGR_Data1D *dattest1,fgUINT8 vtest2,
					    FLGR_Data1D *dattrue, FLGR_Data1D *datfalse) {
#ifdef __SSE2__
  int j;
  fgUINT8 *rowptr_out,*rowptr_t1;
  fgUINT8 *rowptr_true, *rowptr_false;

  __m128i ptst1, ptst2, ptrue, pfalse;
  __m128i mask,one;

  FLGR_DECLARE_FUNCTION;

  one = _mm_set1_epi8((char) 0xFF);
  ptst2 = _mm_set1_epi8((char) vtest2);

  rowptr_out = (fgUINT8 *) datout->array;
  rowptr_t1 = (fgUINT8 *) dattest1->array;
  rowptr_true = (fgUINT8 *) dattrue->array;
  rowptr_false = (fgUINT8 *) datfalse->array;


  for(j=0 ; j<datout->length ; j+=16) {
    ptst1  = _mm_load_si128((__m128i*) rowptr_t1);
    ptrue  = _mm_load_si128((__m128i*) rowptr_true);
    pfalse = _mm_load_si128((__m128i*) rowptr_false);

    mask = _mm_cmpeq_epi8(ptst1,ptst2);
    _mm_maskmoveu_si128(pfalse,mask, (char*) rowptr_out);
    //_mm_mfence();
    mask = _mm_andnot_si128(mask,one);
    _mm_maskmoveu_si128(ptrue,mask, (char*) rowptr_out);


    rowptr_false+=16;
    rowptr_true+=16;
    rowptr_t1+=16;
    rowptr_out+=16;
  }
#endif
}
void flgr1d_replace_I_GE_C_I_I_fast_fgUINT8(FLGR_Data1D *datout,
					    FLGR_Data1D *dattest1,fgUINT8 vtest2,
					    FLGR_Data1D *dattrue, FLGR_Data1D *datfalse) {
#ifdef __SSE2__
  int j;
  fgUINT8 *rowptr_out,*rowptr_t1;
  fgUINT8 *rowptr_true, *rowptr_false;

  __m128i ptst1, ptst2, ptrue, pfalse;
  __m128i mask,one;

  FLGR_DECLARE_FUNCTION;

  one = _mm_set1_epi8((char) 0xFF);
  ptst2 = _mm_set1_epi8((char) vtest2);

  rowptr_out = (fgUINT8 *) datout->array;
  rowptr_t1 = (fgUINT8 *) dattest1->array;
  rowptr_true = (fgUINT8 *) dattrue->array;
  rowptr_false = (fgUINT8 *) datfalse->array;


  for(j=0 ; j<datout->length ; j+=16) {
    ptst1  = _mm_load_si128((__m128i*) rowptr_t1);
    ptrue  = _mm_load_si128((__m128i*) rowptr_true);
    pfalse = _mm_load_si128((__m128i*) rowptr_false);

    ptst1 = _mm_min_epu8(ptst1,ptst2);
    mask = _mm_cmpeq_epi8(ptst1,ptst2);
    _mm_maskmoveu_si128(ptrue,mask, (char*) rowptr_out);
    //_mm_mfence();
    mask = _mm_andnot_si128(mask,one);
    _mm_maskmoveu_si128(pfalse,mask, (char*) rowptr_out);


    rowptr_false+=16;
    rowptr_true+=16;
    rowptr_t1+=16;
    rowptr_out+=16;
  }
#endif
}








void flgr1d_replace_I_LE_C_I_C_fast_fgUINT8(FLGR_Data1D *datout,
					    FLGR_Data1D *dattest1,fgUINT8 vtest2,
					    FLGR_Data1D *dattrue, fgUINT8 vfalse) {
#ifdef __SSE2__
  int j;
  fgUINT8 *rowptr_out,*rowptr_t1;
  fgUINT8 *rowptr_true;

  __m128i ptst1, ptst2, ptrue, pfalse;
  __m128i mask,one;

  FLGR_DECLARE_FUNCTION;

  one = _mm_set1_epi8((char) 0xFF);

  ptst2 = _mm_set1_epi8((char) vtest2);
  pfalse = _mm_set1_epi8((char) vfalse);

  rowptr_out = (fgUINT8 *) datout->array;
  rowptr_t1 = (fgUINT8 *) dattest1->array;
  rowptr_true = (fgUINT8 *) dattrue->array;


  for(j=0 ; j<datout->length ; j+=16) {
    ptst1  = _mm_load_si128((__m128i*) rowptr_t1);
    ptrue  = _mm_load_si128((__m128i*) rowptr_true);

    ptst1 = _mm_max_epu8(ptst1,ptst2);
    mask = _mm_cmpeq_epi8(ptst1,ptst2);
    _mm_maskmoveu_si128(ptrue,mask, (char*) rowptr_out);
    //_mm_mfence();
    mask = _mm_andnot_si128(mask,one);
    _mm_maskmoveu_si128(pfalse,mask, (char*) rowptr_out);


    rowptr_true+=16;
    rowptr_t1+=16;
    rowptr_out+=16;
  }
#endif
}
void flgr1d_replace_I_EQ_C_I_C_fast_fgUINT8(FLGR_Data1D *datout,
					    FLGR_Data1D *dattest1,fgUINT8 vtest2,
					    FLGR_Data1D *dattrue, fgUINT8 vfalse) {
#ifdef __SSE2__
  int j;
  fgUINT8 *rowptr_out,*rowptr_t1;
  fgUINT8 *rowptr_true;

  __m128i ptst1, ptst2, ptrue, pfalse;
  __m128i mask,one;

  FLGR_DECLARE_FUNCTION;

  one = _mm_set1_epi8((char) 0xFF);

  ptst2 = _mm_set1_epi8((char) vtest2);
  pfalse = _mm_set1_epi8((char) vfalse);

  rowptr_out = (fgUINT8 *) datout->array;
  rowptr_t1 = (fgUINT8 *) dattest1->array;
  rowptr_true = (fgUINT8 *) dattrue->array;


  for(j=0 ; j<datout->length ; j+=16) {
    ptst1  = _mm_load_si128((__m128i*) rowptr_t1);
    ptrue  = _mm_load_si128((__m128i*) rowptr_true);

    mask = _mm_cmpeq_epi8(ptst1,ptst2);
    _mm_maskmoveu_si128(ptrue,mask, (char*) rowptr_out);
    //_mm_mfence();
    mask = _mm_andnot_si128(mask,one);
    _mm_maskmoveu_si128(pfalse,mask, (char*) rowptr_out);


    rowptr_true+=16;
    rowptr_t1+=16;
    rowptr_out+=16;
  }
#endif
}
void flgr1d_replace_I_NE_C_I_C_fast_fgUINT8(FLGR_Data1D *datout,
					    FLGR_Data1D *dattest1,fgUINT8 vtest2,
					    FLGR_Data1D *dattrue, fgUINT8 vfalse) {
#ifdef __SSE2__
  int j;
  fgUINT8 *rowptr_out,*rowptr_t1;
  fgUINT8 *rowptr_true;

  __m128i ptst1, ptst2, ptrue, pfalse;
  __m128i mask,one;

  FLGR_DECLARE_FUNCTION;

  one = _mm_set1_epi8((char) 0xFF);

  ptst2 = _mm_set1_epi8((char) vtest2);
  pfalse = _mm_set1_epi8((char) vfalse);

  rowptr_out = (fgUINT8 *) datout->array;
  rowptr_t1 = (fgUINT8 *) dattest1->array;
  rowptr_true = (fgUINT8 *) dattrue->array;


  for(j=0 ; j<datout->length ; j+=16) {
    ptst1  = _mm_load_si128((__m128i*) rowptr_t1);
    ptrue  = _mm_load_si128((__m128i*) rowptr_true);

    mask = _mm_cmpeq_epi8(ptst1,ptst2);
    _mm_maskmoveu_si128(pfalse,mask, (char*) rowptr_out);
    //_mm_mfence();
    mask = _mm_andnot_si128(mask,one);
    _mm_maskmoveu_si128(ptrue,mask, (char*) rowptr_out);


    rowptr_true+=16;
    rowptr_t1+=16;
    rowptr_out+=16;
  }
#endif
}
void flgr1d_replace_I_GE_C_I_C_fast_fgUINT8(FLGR_Data1D *datout,
					    FLGR_Data1D *dattest1,fgUINT8 vtest2,
					    FLGR_Data1D *dattrue, fgUINT8 vfalse) {
#ifdef __SSE2__
  int j;
  fgUINT8 *rowptr_out,*rowptr_t1;
  fgUINT8 *rowptr_true;

  __m128i ptst1, ptst2, ptrue, pfalse;
  __m128i mask,one;

  FLGR_DECLARE_FUNCTION;

  one = _mm_set1_epi8((char) 0xFF);

  ptst2 = _mm_set1_epi8((char) vtest2);
  pfalse = _mm_set1_epi8((char) vfalse);

  rowptr_out = (fgUINT8 *) datout->array;
  rowptr_t1 = (fgUINT8 *) dattest1->array;
  rowptr_true = (fgUINT8 *) dattrue->array;


  for(j=0 ; j<datout->length ; j+=16) {
    ptst1  = _mm_load_si128((__m128i*) rowptr_t1);
    ptrue  = _mm_load_si128((__m128i*) rowptr_true);

    ptst1 = _mm_min_epu8(ptst1,ptst2);
    mask = _mm_cmpeq_epi8(ptst1,ptst2);
    _mm_maskmoveu_si128(ptrue,mask, (char*) rowptr_out);
    //_mm_mfence();
    mask = _mm_andnot_si128(mask,one);
    _mm_maskmoveu_si128(pfalse,mask, (char*) rowptr_out);


    rowptr_true+=16;
    rowptr_t1+=16;
    rowptr_out+=16;
  }
#endif
}










void flgr1d_replace_I_LE_C_C_I_fast_fgUINT8(FLGR_Data1D *datout,
					    FLGR_Data1D *dattest1,fgUINT8 vtest2,
					    fgUINT8 vtrue, FLGR_Data1D *datfalse) {
#ifdef __SSE2__
  int j;
  fgUINT8 *rowptr_out,*rowptr_t1;
  fgUINT8 *rowptr_false;

  __m128i ptst1, ptst2, ptrue, pfalse;
  __m128i mask,one;

  FLGR_DECLARE_FUNCTION;

  one = _mm_set1_epi8((char) 0xFF);

  ptst2 = _mm_set1_epi8((char) vtest2);
  ptrue = _mm_set1_epi8((char) vtrue);

  rowptr_out = (fgUINT8 *) datout->array;
  rowptr_t1 = (fgUINT8 *) dattest1->array;
  rowptr_false = (fgUINT8 *) datfalse->array;


  for(j=0 ; j<datout->length ; j+=16) {
    ptst1  = _mm_load_si128((__m128i*) rowptr_t1);
    pfalse  = _mm_load_si128((__m128i*) rowptr_false);

    ptst1 = _mm_max_epu8(ptst1,ptst2);
    mask = _mm_cmpeq_epi8(ptst1,ptst2);
    _mm_maskmoveu_si128(ptrue,mask, (char*) rowptr_out);
    //_mm_mfence();
    mask = _mm_andnot_si128(mask,one);
    _mm_maskmoveu_si128(pfalse,mask, (char*) rowptr_out);


    rowptr_false+=16;
    rowptr_t1+=16;
    rowptr_out+=16;
  }
#endif
}
void flgr1d_replace_I_EQ_C_C_I_fast_fgUINT8(FLGR_Data1D *datout,
					    FLGR_Data1D *dattest1,fgUINT8 vtest2,
					    fgUINT8 vtrue, FLGR_Data1D *datfalse) {
#ifdef __SSE2__
  int j;
  fgUINT8 *rowptr_out,*rowptr_t1;
  fgUINT8 *rowptr_false;

  __m128i ptst1, ptst2, ptrue, pfalse;
  __m128i mask,one;

  FLGR_DECLARE_FUNCTION;

  one = _mm_set1_epi8((char) 0xFF);

  ptst2 = _mm_set1_epi8((char) vtest2);
  ptrue = _mm_set1_epi8((char) vtrue);

  rowptr_out = (fgUINT8 *) datout->array;
  rowptr_t1 = (fgUINT8 *) dattest1->array;
  rowptr_false = (fgUINT8 *) datfalse->array;


  for(j=0 ; j<datout->length ; j+=16) {
    ptst1  = _mm_load_si128((__m128i*) rowptr_t1);
    pfalse = _mm_load_si128((__m128i*) rowptr_false);

    mask = _mm_cmpeq_epi8(ptst1,ptst2);
    _mm_maskmoveu_si128(ptrue,mask, (char*) rowptr_out);
    //_mm_mfence();
    mask = _mm_andnot_si128(mask,one);
    _mm_maskmoveu_si128(pfalse,mask, (char*) rowptr_out);


    rowptr_false+=16;
    rowptr_t1+=16;
    rowptr_out+=16;
  }
#endif
}
void flgr1d_replace_I_NE_C_C_I_fast_fgUINT8(FLGR_Data1D *datout,
					    FLGR_Data1D *dattest1,fgUINT8 vtest2,
					    fgUINT8 vtrue, FLGR_Data1D *datfalse) {
#ifdef __SSE2__
  int j;
  fgUINT8 *rowptr_out,*rowptr_t1;
  fgUINT8 *rowptr_false;

  __m128i ptst1, ptst2, ptrue, pfalse;
  __m128i mask,one;

  FLGR_DECLARE_FUNCTION;

  one = _mm_set1_epi8((char) 0xFF);

  ptst2 = _mm_set1_epi8((char) vtest2);
  ptrue = _mm_set1_epi8((char) vtrue);

  rowptr_out = (fgUINT8 *) datout->array;
  rowptr_t1 = (fgUINT8 *) dattest1->array;
  rowptr_false = (fgUINT8 *) datfalse->array;


  for(j=0 ; j<datout->length ; j+=16) {
    ptst1  = _mm_load_si128((__m128i*) rowptr_t1);
    pfalse = _mm_load_si128((__m128i*) rowptr_false);

    mask = _mm_cmpeq_epi8(ptst1,ptst2);
    _mm_maskmoveu_si128(pfalse,mask, (char*) rowptr_out);
    //_mm_mfence();
    mask = _mm_andnot_si128(mask,one);
    _mm_maskmoveu_si128(ptrue,mask, (char*) rowptr_out);


    rowptr_false+=16;
    rowptr_t1+=16;
    rowptr_out+=16;
  }
#endif
}
void flgr1d_replace_I_GE_C_C_I_fast_fgUINT8(FLGR_Data1D *datout,
					    FLGR_Data1D *dattest1,fgUINT8 vtest2,
					    fgUINT8 vtrue, FLGR_Data1D *datfalse) {
#ifdef __SSE2__
  int j;
  fgUINT8 *rowptr_out,*rowptr_t1;
  fgUINT8 *rowptr_false;

  __m128i ptst1, ptst2, ptrue, pfalse;
  __m128i mask,one;

  FLGR_DECLARE_FUNCTION;

  one = _mm_set1_epi8((char) 0xFF);

  ptst2 = _mm_set1_epi8((char) vtest2);
  ptrue = _mm_set1_epi8((char) vtrue);

  rowptr_out = (fgUINT8 *) datout->array;
  rowptr_t1 = (fgUINT8 *) dattest1->array;
  rowptr_false = (fgUINT8 *) datfalse->array;


  for(j=0 ; j<datout->length ; j+=16) {
    ptst1  = _mm_load_si128((__m128i*) rowptr_t1);
    pfalse  = _mm_load_si128((__m128i*) rowptr_false);

    ptst1 = _mm_min_epu8(ptst1,ptst2);
    mask = _mm_cmpeq_epi8(ptst1,ptst2);
    _mm_maskmoveu_si128(ptrue,mask, (char*) rowptr_out);
    //_mm_mfence();
    mask = _mm_andnot_si128(mask,one);
    _mm_maskmoveu_si128(pfalse,mask, (char*) rowptr_out);


    rowptr_false+=16;
    rowptr_t1+=16;
    rowptr_out+=16;
  }
#endif
}









void flgr1d_replace_I_LE_C_C_C_fast_fgUINT8(FLGR_Data1D *datout,
					    FLGR_Data1D *dattest1,fgUINT8 vtest2,
					    fgUINT8 vtrue, fgUINT8 vfalse) {
#ifdef __SSE2__
  int j;
  fgUINT8 *rowptr_out,*rowptr_t1;

  __m128i ptst1, ptst2, ptrue, pfalse;
  __m128i mask,one;

  FLGR_DECLARE_FUNCTION;

  one = _mm_set1_epi8((char) 0xFF);

  ptst2 = _mm_set1_epi8((char) vtest2);
  ptrue = _mm_set1_epi8((char) vtrue);
  pfalse = _mm_set1_epi8((char) vfalse);

  rowptr_out = (fgUINT8 *) datout->array;
  rowptr_t1 = (fgUINT8 *) dattest1->array;


  for(j=0 ; j<datout->length ; j+=16) {
    ptst1  = _mm_load_si128((__m128i*) rowptr_t1);

    ptst1 = _mm_max_epu8(ptst1,ptst2);
    mask = _mm_cmpeq_epi8(ptst1,ptst2);
    _mm_maskmoveu_si128(ptrue,mask, (char*) rowptr_out);
    //_mm_mfence();
    mask = _mm_andnot_si128(mask,one);
    _mm_maskmoveu_si128(pfalse,mask, (char*) rowptr_out);


    rowptr_t1+=16;
    rowptr_out+=16;
  }
#endif
}
void flgr1d_replace_I_EQ_C_C_C_fast_fgUINT8(FLGR_Data1D *datout,
					    FLGR_Data1D *dattest1,fgUINT8 vtest2,
					    fgUINT8 vtrue, fgUINT8 vfalse) {
#ifdef __SSE2__
  int j;
  fgUINT8 *rowptr_out,*rowptr_t1;

  __m128i ptst1, ptst2, ptrue, pfalse;
  __m128i mask,one;

  FLGR_DECLARE_FUNCTION;

  one = _mm_set1_epi8((char) 0xFF);

  ptst2 = _mm_set1_epi8((char) vtest2);
  ptrue = _mm_set1_epi8((char) vtrue);
  pfalse = _mm_set1_epi8((char) vfalse);

  rowptr_out = (fgUINT8 *) datout->array;
  rowptr_t1 = (fgUINT8 *) dattest1->array;


  for(j=0 ; j<datout->length ; j+=16) {
    ptst1  = _mm_load_si128((__m128i*) rowptr_t1);

    mask = _mm_cmpeq_epi8(ptst1,ptst2);
    _mm_maskmoveu_si128(ptrue,mask, (char*) rowptr_out);
    //_mm_mfence();
    mask = _mm_andnot_si128(mask,one);
    _mm_maskmoveu_si128(pfalse,mask, (char*) rowptr_out);


    rowptr_t1+=16;
    rowptr_out+=16;
  }
#endif
}
void flgr1d_replace_I_NE_C_C_C_fast_fgUINT8(FLGR_Data1D *datout,
					    FLGR_Data1D *dattest1,fgUINT8 vtest2,
					    fgUINT8 vtrue, fgUINT8 vfalse) {
#ifdef __SSE2__
  int j;
  fgUINT8 *rowptr_out,*rowptr_t1;

  __m128i ptst1, ptst2, ptrue, pfalse;
  __m128i mask,one;

  FLGR_DECLARE_FUNCTION;

  one = _mm_set1_epi8((char) 0xFF);

  ptst2 = _mm_set1_epi8((char) vtest2);
  ptrue = _mm_set1_epi8((char) vtrue);
  pfalse = _mm_set1_epi8((char) vfalse);

  rowptr_out = (fgUINT8 *) datout->array;
  rowptr_t1 = (fgUINT8 *) dattest1->array;


  for(j=0 ; j<datout->length ; j+=16) {
    ptst1  = _mm_load_si128((__m128i*) rowptr_t1);

    mask = _mm_cmpeq_epi8(ptst1,ptst2);
    _mm_maskmoveu_si128(pfalse,mask, (char*) rowptr_out);
    //_mm_mfence();
    mask = _mm_andnot_si128(mask,one);
    _mm_maskmoveu_si128(ptrue,mask, (char*) rowptr_out);


    rowptr_t1+=16;
    rowptr_out+=16;
  }
#endif
}
void flgr1d_replace_I_GE_C_C_C_fast_fgUINT8(FLGR_Data1D *datout,
					    FLGR_Data1D *dattest1,fgUINT8 vtest2,
					    fgUINT8 vtrue, fgUINT8 vfalse) {
#ifdef __SSE2__
  int j;
  fgUINT8 *rowptr_out,*rowptr_t1;

  __m128i ptst1, ptst2, ptrue, pfalse;
  __m128i mask,one;

  FLGR_DECLARE_FUNCTION;

  one = _mm_set1_epi8((char) 0xFF);

  ptst2 = _mm_set1_epi8((char) vtest2);
  ptrue = _mm_set1_epi8((char) vtrue);
  pfalse = _mm_set1_epi8((char) vfalse);

  rowptr_out = (fgUINT8 *) datout->array;
  rowptr_t1 = (fgUINT8 *) dattest1->array;


  for(j=0 ; j<datout->length ; j+=16) {
    ptst1  = _mm_load_si128((__m128i*) rowptr_t1);

    ptst1 = _mm_min_epu8(ptst1,ptst2);
    mask = _mm_cmpeq_epi8(ptst1,ptst2);
    _mm_maskmoveu_si128(ptrue,mask, (char*) rowptr_out);
    //_mm_mfence();
    mask = _mm_andnot_si128(mask,one);
    _mm_maskmoveu_si128(pfalse,mask, (char*) rowptr_out);


    rowptr_t1+=16;
    rowptr_out+=16;
  }
#endif
}











//! @}
